package com.huluobo.a2201mvvm_imdemo.contact;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.huluobo.a2201mvvm_imdemo.chat.ChatActivity;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.modules.contact.EaseContactListFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ContactFragment extends EaseContactListFragment {
    private List<String> usernames = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EMClient.getInstance().contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                //由于拿到的value与显示的list顺序不符,所以需要排个序
                Collections.sort(value);
                usernames.addAll(value);
            }

            @Override
            public void onError(int error, String errorMsg) {
                Log.i("TagA", "error:" + errorMsg + ",code:" + error);
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        String conversationId = usernames.get(position);
        Intent intent = new Intent(requireContext(), ChatActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("conversationId", conversationId);
        bundle.putInt("chatType", 1);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}